﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Anthony.Library;
using System.Drawing;
using Formsy.Common;


namespace Formsy.Providers.FileSystem
{

    public class ImageThumbnailProvider : FileSystemProvider
    {
        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
        }


        /// <summary>
        /// Saves a thumbnail image.
        /// </summary>
        /// <returns></returns>
        public override List<FormsyFile> Save(string formName, int entryId, List<FormsyFile> files, string folderPath)
        {
            List<FormsyFile> thumbnails = new List<FormsyFile>();

            foreach (var f in files)
            {
                string filename = entryId.ToString() + "_" + f.FieldName + "." + f.Extension;
                string thumbName = entryId.ToString() + "_" + f.FieldName + "_thumb." + f.Extension;

                FileSystemLibrary.SaveImage(folderPath, filename, f.Bytes);
                FileSystemLibrary.SaveThumbnailImage(folderPath, thumbName, f.Bytes, 100, 100);
                f.Filename = filename;
                FormsyFile thumb = new FormsyFile(f.FieldName, null, f.Extension);
                thumb.Filename = thumbName;
                thumb.FieldName = thumb.FieldName + "_thumb";
                thumbnails.Add(thumb);
            }

            // combine lists
            files.AddRange(thumbnails);

            return files;
        }

    }
}
